﻿using System;
using System.Collections.Generic;

namespace zijian666.Converts.Core;

/// <summary>
/// 异常收集器
/// </summary>
public sealed class ExceptionCollector : List<Exception>
{
    /// <summary>
    /// 转换为 
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    public AggregateException ToException(string message) => new(message, this);

    /// <summary>
    /// 收集异常
    /// </summary>
    public static void Collect(ref ExceptionCollector? ex1, Exception? ex2)
    {
        if (ex2 is null)
        {
            return;
        }
        if (ex1 is null)
        {
            ex1 = new ExceptionCollector() { ex2 };
        }
        else
        {
            ex1.Add(ex2);
        }
    }
}